package com.fy.service.imbusiness.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.fy.common.dto.BaseIdDto;
import com.fy.common.dto.GroupMemberRoleDto;
import com.fy.common.entity.SkImGroupMember;
import com.fy.common.util.TokenUtil;
import com.fy.common.vo.GroupMemberVo;
import com.fy.framework.common.model.R;
import com.fy.framework.component.config.LocaleMessage;
import com.fy.service.service.SkImGroupMemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author zhshuo
 * @date 2021/12/17 18:33
 */
@Slf4j
@RestController
@AllArgsConstructor
@Api(tags = "群组成员")
@RequestMapping("/group")
public class SkImGroupMemberController {

    LocaleMessage localeMessage;

    SkImGroupMemberService groupMemberService;

    /**
     * 退群
     *
     * @return
     */
    @PostMapping("quit")
    @ApiOperation("用户主动退群")
    @ApiImplicitParams({@ApiImplicitParam(name = "userId", value = "被踢的用户ID", required = true)})
    public R<?> quit(@RequestBody JSONObject groupId) {
        try {
            groupMemberService.quit(groupId.getLong("groupId"));
            return R.success(localeMessage.getMessage("im.common.successful"));
        } catch (Exception e) {
            log.error("error", e.getCause());
            return R.failedAlter(localeMessage.getMessage("im.common.failedful"));
        }
    }

    /**
     * 踢出群
     *
     * @return
     */
    @PostMapping("kickOut")
    @ApiOperation("踢出群")
    @ApiImplicitParams({@ApiImplicitParam(name = "groupId", value = "群ID", required = true),
            @ApiImplicitParam(name = "userId", value = "被踢的用户ID", required = true)})
    public R<?> kickOut(@RequestBody JSONObject param) {
        try {
            Long groupId = param.getLong("groupId");
            Long userId = param.getLong("userId");
            groupMemberService.kickOut(groupId, userId);
            return R.success(localeMessage.getMessage("im.common.successful"));
        } catch (Exception e) {
            log.error("error", e.getCause());
            return R.failedAlter(localeMessage.getMessage("im.common.failedful"));
        }
    }

    /**
     * 群成员角色设置
     * @return {@link R}
     */
    @PostMapping("memberRole")
    @ApiOperation("群成员角色设置")
    public R<?> memberRole(@RequestBody GroupMemberRoleDto memberRoleDto){
        return groupMemberService.memberRole(memberRoleDto);
    }

    /**
     *
     * @return
     */
    @PostMapping("memberInfo")
    @ApiOperation("成员信息")
    public R<?> memberInfo(@RequestBody BaseIdDto baseIdDto){
        try {
            SkImGroupMember groupMember = groupMemberService.selectOne(new EntityWrapper<SkImGroupMember>().eq("sk_im_group_id", baseIdDto.getId()).eq("sk_im_user_id", TokenUtil.getUserId()));
            return R.success(groupMember);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return R.failedAlter(localeMessage.getMessage("im.common.failedful"));
        }
    }

    /**
     *
     * @return
     */
    @PostMapping("groupMember")
    @ApiOperation("群成员查询")
    public R<List<GroupMemberVo>> groupMember(@RequestBody BaseIdDto baseIdDto){
        try {
            List<GroupMemberVo> memberVos = groupMemberService.groupMember(baseIdDto.getId());
            return R.success(memberVos);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return R.failedAlter(localeMessage.getMessage("im.common.failedful"));
        }
    }

}